package com.resys.server.controller.customer;

import com.resys.entity.po.Device;
import com.resys.entity.po.Meetingroom;
import com.resys.entity.po.Roomdevice;
import com.resys.entity.query.MeetingroomQuery;
import com.resys.entity.query.RoomdeviceQuery;
import com.resys.entity.vo.ResponseVO;
import com.resys.exception.BusinessException;
import com.resys.server.controller.ABaseController;
import com.resys.service.DeviceService;
import com.resys.service.MeetingroomService;
import com.resys.service.RoomdeviceService;
import com.resys.service.impl.DeviceServiceImpl;
import com.resys.service.impl.RoomshowServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

@Slf4j
@RestController("meetingRoomController4customer")
@RequestMapping("/customer/meetingRoom")
@Api(tags = "客户模块-会议室接口")
public class MeetingRoomController extends ABaseController {
    @Resource
    private MeetingroomService meetingroomService;
    @Resource
    private RoomdeviceService roomdeviceService;
    @Resource
    private DeviceServiceImpl deviceService;
    @Resource
    private RoomshowServiceImpl roomshowService;
    private final String UPLOAD_PATH = System.getProperty("user.dir") + "/static/meetingroom/";
    @ApiOperation("模糊搜索会议室")
    @GetMapping("/search")
    public ResponseVO search(String FuzzyName){
        MeetingroomQuery query = new MeetingroomQuery();
        query.setNameFuzzy(FuzzyName);
        List<Meetingroom> list = meetingroomService.findListByParam(query);
        return getSuccessResponseVO(list);

    }
    @ApiOperation("获取会议室列表")
    @GetMapping
    public ResponseVO list(){
        MeetingroomQuery query  = new MeetingroomQuery();
        List<Meetingroom> list = meetingroomService.findListByParam(query);
        for(Meetingroom room : list ){
            if(room.getStatus()==4){
                list.remove(room);
            }
        }
        return getSuccessResponseVO(list);
    }
    @ApiOperation("获取指定会议室的设备信息")
    @GetMapping("/device")
    public ResponseVO getDevice(Integer roomId){
        RoomdeviceQuery query = new RoomdeviceQuery();
        query.setRoomId(roomId);
        List<Roomdevice> list = roomdeviceService.findListByParam(query);
        List<Device> result = new ArrayList<>();
        Device divice = new Device();

        for (Roomdevice roomdevice : list) {
            divice = deviceService.getDeviceByDeviceId( roomdevice.getDeviceId());
            result.add(divice);
        }
        return  getSuccessResponseVO(result);
    }
    @ApiOperation("获取指定会议室的内部环境图片数量")
    @GetMapping("/imageCount")
    public ResponseVO getImageCount(Integer roomId){
        Integer count = roomshowService.getImageCount(roomId);
        return getSuccessResponseVO(count);
    }
    @ApiOperation("获取指定会议室的第几张图片")
    @GetMapping("/image")
    public ResponseEntity<byte[]> getImg(Integer roomId, Integer index){
        String filename =  roomshowService.getImgName(roomId, index);
        Path path= Paths.get(UPLOAD_PATH);
        try {
            // 遍历目录查找文件
            Optional<Path> foundFile;
            try (Stream<Path> stream = Files.walk(path)) {
                foundFile = stream
                        .filter(Files::isRegularFile)
                        .filter(p -> p.getFileName().toString().equals(filename))
                        .findFirst();
            }

            // 读取文件内容
            Path filePath = foundFile.get();
            byte[] imageBytes = Files.readAllBytes(filePath);

            // 根据后缀确定MIME类型
            String fileExtension = filename.substring(filename.lastIndexOf('.') + 1);

            // 返回图片数据
            return ResponseEntity.ok()
                    .contentType(fileExtension.equalsIgnoreCase("png") ? MediaType.IMAGE_PNG : MediaType.IMAGE_JPEG)
                    .body(imageBytes);

        } catch (IOException e) {
            throw new BusinessException("会议室图片读取失败", e);
        }

    }
}
